//
//  YAYIPerformanceDetailViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/10.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIPerformanceDetailViewController.h"

#import "YAYIPerformanceDetailCell.h"
#import "YAYIPerformanceDetailModel.h"

@interface YAYIPerformanceDetailViewController ()

// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic, strong) NSMutableArray *servicePatientArray;

/** 分页相关 */
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYIPerformanceDetailViewController

- (NSMutableArray *)servicePatientArray
{
    if (nil == _servicePatientArray) {
        _servicePatientArray = [NSMutableArray array];
    }
    return _servicePatientArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"服务人次";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    [self getServicePatientList];
    
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself getServicePatientList];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreServicePatientList];
    }];
//    [self.tableView.mj_footer setAutomaticallyHidden:YES];
    
    [self.tableView.mj_header beginRefreshing];
}


/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有数据";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-50);
    }];
}


#pragma mark - 获取数据
- (void)getServicePatientList
{
    
    [self.servicePatientArray removeAllObjects];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = self.typeStr;
    
    [YAYINetWorking getWithURL:me_servicePatientList param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            }else{
                
                NSArray *dataArray = dict[@"data"];
                for (NSDictionary *itemDict in dataArray) {
                    YAYIPerformanceDetailModel *model = [[YAYIPerformanceDetailModel alloc] init];

                    [model setValuesForKeysWithDictionary:itemDict];
                    
                    [self.servicePatientArray addObject:model];
                }
                
                self.curPage = 1;
                
                [self.tableView.mj_header endRefreshing];
                if (self.servicePatientArray.count < 10) {
                    
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer setState:MJRefreshStateIdle];
                    self.tableView.mj_footer.hidden = NO;
                }
                
                if (0 == self.servicePatientArray.count) {
                    [self showNoDataFlag:YES];
                    
                }else{
                    [self showNoDataFlag:NO];
                }
                
                [self.tableView reloadData];
            }
            
        }else{
            
            YAYILog(@"erro:%@", error);
        }
        
        // 取消显示hud
        //        [SVProgressHUD dismiss];
    }];
}


- (void)getMoreServicePatientList
{
    
    self.curPage++;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(self.curPage);
    param[@"type"] = self.typeStr;
    
    [YAYINetWorking getWithURL:me_servicePatientList param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
            }else{
                
                NSArray *dataArray = dict[@"data"];
                for (NSDictionary *itemDict in dataArray) {
                    YAYIPerformanceDetailModel *model = [[YAYIPerformanceDetailModel alloc] init];
                    
                    [model setValuesForKeysWithDictionary:itemDict];
                    [self.servicePatientArray addObject:model];
                }
                
                if (dataArray.count < 10) {
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                
                [self.tableView reloadData];
            }
            
        }else{
            
            YAYILog(@"erro:%@", error);
        }
        
        // 取消显示hud
        //        [SVProgressHUD dismiss];
    }];
}


#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.servicePatientArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIPerformanceDetailModel *model = self.servicePatientArray[indexPath.row];
    
    YAYIPerformanceDetailCell *cell = [YAYIPerformanceDetailCell cellWith:tableView];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end
